package com.weixin.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.weixin.entity.Store;
import com.weixin.service.StoreService;
import com.weixin.service.impl.StoreServiceImpl;
import com.weixin.util.PageUtil;


@WebServlet("/StoreServlet/*")
public class StoreServlet extends HttpServlet {
StoreService service = new StoreServiceImpl();
	
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}
	
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String path = request.getPathInfo().substring(1);
		System.out.println(path);
		
		if("list".equals(path)) {
			list(request,response);
		}else if("add".equals(path)) {
			add(request,response);
		}
		else if("preEdit".equals(path)) {
			preEdit(request,response);
		}
		else if("edit".equals(path)) {
			edit(request,response);
		}else if("delete".equals(path)) {
			delete(request,response);
		}else if("listByPage".equals(path)) {
			listByPage(request,response);
		}else if("deleteAjax".equals(path)) {
			deleteAjax(request,response);
		}
		else if("preAdd".equals(path)) {
			preAdd(request,response);
		}
	}

	private void deleteAjax(HttpServletRequest request, HttpServletResponse response) {
		//根据传入的参数storehouse_id删除数据：
				String storehouse_id = request.getParameter("storehouse_id");
				System.out.println(storehouse_id);
				try {
					PrintWriter out = response.getWriter();
					if(service.deleteById(Integer.valueOf(storehouse_id))) {
						//删除成功
						out.print("1");
					}else {
						//删除失败
						out.print("0");
					}
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		
	}

	private void listByPage(HttpServletRequest request, HttpServletResponse response) {
		String pageNum = request.getParameter("pageNum");
		try {
			//仓库数据：
			List<Store> storelist = service.ListByPage(PageUtil.PAGE_SIZE,Integer.valueOf(pageNum));
			request.setAttribute("storelist",storelist);
			//分页条数据
			request.setAttribute("pageCount", Math.ceil(service.getTotal()*1.0/PageUtil.PAGE_SIZE));
			request.setAttribute("pageNum", pageNum);
			request.getRequestDispatcher("/page/user/storehouseManage/store_list.jsp").forward(request, response);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
	private void delete(HttpServletRequest request, HttpServletResponse response) {
		//根据传入的参数storehouse_id删除数据：
		String storehouse_id = request.getParameter("storehouse_id");
		System.out.println(storehouse_id);
		try {
			if(service.deleteById(Integer.valueOf(storehouse_id))) {
				//删除成功
				request.setAttribute("deleteMessage", "删除成功");
			}else {
				//删除失败
				request.setAttribute("deleteMessage", "删除失败");
			}
			list(request, response);
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void edit(HttpServletRequest request, HttpServletResponse response) {
		//开始修改：
		
		String storehouse_name = request.getParameter("storehouse_name");
		String creation_date = request.getParameter("creation_date");
		String total_content = request.getParameter("total_content");
		String residual_content = request.getParameter("residual_content");
		String remark = request.getParameter("remark");
		String responsible_person = request.getParameter("responsible_person");
		
		String storehouse_id = request.getParameter("storehouse_id");
		
		
		
		try {
			Store store = service.getById(Integer.valueOf(storehouse_id));
			store.setStorehouse_name(storehouse_name);
			
			store.setCreation_date(new SimpleDateFormat("yyyy-MM-dd").parse(creation_date));
			
			store.setTotal_content(Integer.valueOf(total_content));
			store.setResidual_content(Integer.valueOf(residual_content));
			store.setRemark(remark);
			store.setResponsible_person(responsible_person);
			
			
			if(service.update(store)) {
				//修改成功：
				request.setAttribute("updatemessage", "seccess");
			}else {
				//修改失败：
				request.setAttribute("updatemessage", "error");
			}
			//仓库数据：
			List<Store> storelist = service.ListByPage(PageUtil.PAGE_SIZE,1);
			request.setAttribute("storelist",storelist);
			//分页条数据
			request.setAttribute("pageCount", Math.ceil(service.getTotal()*1.0/PageUtil.PAGE_SIZE));
			request.setAttribute("pageNum",1);
			request.getRequestDispatcher("/page/user/storehouseManage/store_list.jsp").forward(request, response);
			
			
		} catch (Exception e) {

			e.printStackTrace();
		} 
		//调用service:
		
		
	}

	private void preEdit(HttpServletRequest request, HttpServletResponse response) {
		//拿到storehouse_id:
		String storehouse_id = request.getParameter("storehouse_id");
		
		//通过storehouse_id查找这个仓库：
		try {
			Store store = service.getById(Integer.valueOf(storehouse_id));
			request.setAttribute("store", store);
			System.out.println("准备修改："+store);
			request.getRequestDispatcher("/page/user/storehouseManage/store_edit.jsp").forward(request, response);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void add(HttpServletRequest request, HttpServletResponse response) {
		//接收参数
		String storehouse_name = request.getParameter("storehouse_name");
		String creation_date = request.getParameter("create_date");
		String total_content = request.getParameter("total_content");
		String residual_content = request.getParameter("residual_content");
		String remark = request.getParameter("remark");
		String responsible_person = request.getParameter("responsible_person");
		
		//把参数封装到store对象
		Store store = new Store();
		store.setStorehouse_name(storehouse_name);
		try {
//			SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
//			Date data=null;
// 			data=sf.parse(creation_date);
//			store.setCreation_date(data);
			store.setCreation_date(new SimpleDateFormat("yyyy-MM-dd").parse(creation_date));
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		store.setTotal_content(Integer.valueOf(total_content));
		store.setResidual_content(Integer.valueOf(residual_content));
		store.setRemark(remark);
		store.setResponsible_person(responsible_person);
		try {
			if(service.insert(store)) {
				//添加成功：
				request.setAttribute("message", "seccess");
			}else {
				//添加失败：
				request.setAttribute("message", "error");
			}
			//list(request, response);
			//request.getRequestDispatcher("/page/user/storehouseManage/store_add.jsp").forward(request, response);
			//仓库数据：
			List<Store> storelist = service.ListByPage(PageUtil.PAGE_SIZE,1);
			request.setAttribute("storelist",storelist);
			//分页条数据
			request.setAttribute("pageCount", Math.ceil(service.getTotal()*1.0/PageUtil.PAGE_SIZE));
			request.setAttribute("pageNum",1);
			request.getRequestDispatcher("/page/user/storehouseManage/store_list.jsp").forward(request, response);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}


	
	private void preAdd(HttpServletRequest request, HttpServletResponse response) {
		try {
			request.getRequestDispatcher("/page/user/storehouseManage/store_add.jsp").forward(request, response);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void list(HttpServletRequest request, HttpServletResponse response) {
		try {
			List<Store> storelist = service.getAll();
			
			request.setAttribute("storelist",storelist);
			request.getRequestDispatcher("/page/user/storehouseManage/store_list.jsp").forward(request, response);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	
		
		
	}


